import numpy as Math 

"""
坐标转换类  
"""
pi = 3.14159265358979324;
a = 6378245.0;
ee = 0.00669342162296594323;
x_pi = 3.14159265358979324 * 3000.0 / 180.0;

def gcj2bd(lat, lon) :
    x = lon
    y = lat;
    z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
    theta = Math.arctan2(y, x) + 0.000003 * Math.cos(x * x_pi);
    bd_lon = z * Math.cos(theta) + 0.0065;
    bd_lat = z * Math.sin(theta) + 0.006;

    return [bd_lat, bd_lon];


def bd2gcj(lat, lon):
    x = lon - 0.0065
    y = lat - 0.006;
    z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
    theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
    gg_lon = z * Math.cos(theta);
    gg_lat = z * Math.sin(theta);
    return [gg_lat, gg_lon ];

def wgs2gcj(lat, lon):
    dLat = transformLat(lon - 105.0, lat - 35.0);
    dLon = transformLon(lon - 105.0, lat - 35.0);
    radLat = lat / 180.0 * pi;
    magic = Math.sin(radLat);
    magic = 1 - ee * magic * magic;
    sqrtMagic = Math.sqrt(magic);
    dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
    dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
    mgLat = lat + dLat;
    mgLon = lon + dLon;
    loc = [ mgLat, mgLon ];

    return loc;

def transformLat(lat, lon):
    ret = -100.0 + 2.0 * lat + 3.0 * lon + 0.2 * lon * lon + 0.1 * lat * lon + 0.2 * Math.sqrt(Math.abs(lat));
    ret += (20.0 * Math.sin(6.0 * lat * pi) + 20.0 * Math.sin(2.0 * lat * pi)) * 2.0 / 3.0;
    ret += (20.0 * Math.sin(lon * pi) + 40.0 * Math.sin(lon / 3.0 * pi)) * 2.0 / 3.0;
    ret += (160.0 * Math.sin(lon / 12.0 * pi) + 320 * Math.sin(lon * pi / 30.0)) * 2.0 / 3.0;
    return ret;

def transformLon(lat, lon):
    ret = 300.0 + lat + 2.0 * lon + 0.1 * lat * lat + 0.1 * lat * lon + 0.1 * Math.sqrt(Math.abs(lat));
    ret += (20.0 * Math.sin(6.0 * lat * pi) + 20.0 * Math.sin(2.0 * lat * pi)) * 2.0 / 3.0;
    ret += (20.0 * Math.sin(lat * pi) + 40.0 * Math.sin(lat / 3.0 * pi)) * 2.0 / 3.0;
    ret += (150.0 * Math.sin(lat / 12.0 * pi) + 300.0 * Math.sin(lat / 30.0 * pi)) * 2.0 / 3.0;
    return ret;

def  wgs2bd(lat, lon):
    wgs2gcjv = wgs2gcj(lat, lon)
    gcj2bdv = gcj2bd(wgs2gcjv[0], wgs2gcjv[1])
    return gcj2bdv

if __name__ == "__main__":
    print(wgs2bd(114.016,22.51682))